//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { Image as VanImage } from "vant";
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {
    VanImage
  },
  data() {
    return {
      giftInfo:{},
      mainInfo:{},
      giftNum:1,
      packageTitle:'',
      packageId:'',
      clientWidths: "",
      giftList:[],
      page_template: 1,
      combination:{},
      otherPackages: [],
      //显示购结束时间
      timeStatus: false,
      endTime: 0,
      endTimeStr: "",
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getpackage() {
      $http
        .get(
          "plugin.goods-package.frontend.package.package.index",
          {
            package_id: this.packageId
          },
          "添加中..."
        )
        .then(response => {
          if (response.result == 1) {
            let data = response.data;
            this.getShareData(data);
            this.carousels = data.carousels;
            this.packageTitle = data.title;
            this.combination = data.combination;
            this.otherPackages = data.other_packages;
            if(data.title) {
              this.fun.setWXTitle(data.title);
            }
            this.timeStatus = data.limit_time_status == 1 ? true : false;
            this.endTime = new Date(parseInt(data.end_time) * 1000);
            this.endTimeStr = String(this.endTime);
            this.giftInfo = data;
          } else {
            Toast(response.msg);
            this.$router.push(this.fun.getUrl("home", {}));
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    //倒计时结束后
    salePriceTime() {
      this.timeStatus = false;
    },
    //跳到礼包套餐商品列表
    gogoodsDetailBtn(goodsid) {
      // if(this.page_template) return;
      this.$router.push(this.fun.getUrl("giftGoodsList", { id: this.packageId}));
    },
    giftData() {
      $http
        .get(
          "plugin.goods-package.frontend.package.package.combination-goods",
          {
            package_id: this.packageId,
          },
          "添加中..."
        )
        .then(response => {
          if (response.result == 1) {
            let res = response.data;
            this.giftList = res.list;
          } else {
            this.$dialog.alert({
              message: response.msg
            });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    gopackgoods(otherpid,type) {
      if(type == 1) {
        this.$router.push(this.fun.getUrl("giftIndex", { id: otherpid }));
      } else {
        this.$router.push(this.fun.getUrl("packageGoods", { id: otherpid }));
      }
      
    },
     //提交订单
    submitGoods() {
      let json = {};
      let cartIds = [];
      json.package_id = this.packageId;
      json.goods_list = [];
      this.giftList.map((item,index) => {
        cartIds.push(item.goods?.id);
        let goodsJson = {};
        goodsJson.goods_id = item.goods?.id;
        goodsJson.option_id = '';
        if(item.goods?.has_option) {
          goodsJson.option_id = item.goods?.has_many_options[0].id;
        }
        goodsJson.total = 1;
        json.goods_list.push(goodsJson);
      })
      this.$router.push(
        this.fun.getUrl("goodsorder",{
          packagJson: json,
        }, {
          tag: "giftBuy",
          goods_ids: JSON.stringify(cartIds)
        })
      );
    },
    //获取装修分享数据
    getShareData(data) {
      this.mainInfo = {
        name: data.share_title,
        thumb: data.share_thumb,
        introduce: data.share_desc
      };
      // this.initShare();
      this.fun.wxShare(
        "",
        {},
        {
          title: this.mainInfo.name,
          imgUrl: this.mainInfo.thumb,
          description: this.mainInfo.introduce
        },
        data => {
          // if (!this.fun.isTextEmpty(data.shop && data.shop.shop)) {
          //   this.initCservice(data.shop.shop.cservice); //客服重新赋值
          // }
        }
      );
    },
    //分享
    initShare() {
      let that = this;
      let json = {
        url:
          this.fun.isIosOrAndroid() === "android"
            ? window.location.href
            : window.initUrl,
        mid: this.fun.getKeyByMid()
      };
      $http
        .post("member.member.wxJsSdkConfig", json)
        .then(response => {
          if (response.result === 1) {
            if (response.data.config) {
              that.share(response.data);
            }
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    share(data) {
      let that = this;
      wx.config(data.config);
      wx.ready(function() {
        let _title = that.fun.isTextEmpty(that.mainInfo.name)? data.share.title : that.mainInfo.name;
        let _link = document.location.href + "&share_tag=2";
        _link = that.fun.isMid(_link, data.info.uid);
        let _imgUrl = that.fun.isTextEmpty(that.mainInfo.thumb)? data.share.icon : that.mainInfo.thumb;
        let _desc = that.fun.isTextEmpty(that.mainInfo.introduce)? data.share.desc : that.mainInfo.introduce;
        wx.showOptionMenu();
        wx.onMenuShareTimeline({
          title: _title, // 分享标题
          link: _link, // 分享链接
          imgUrl: _imgUrl, // 分享图标
          success: function() {
            Toast("分享成功");
          },
          cancel: function() {
            Toast("取消分享");
          }
        });

        wx.onMenuShareAppMessage({
          title: _title, // 分享标题
          desc: _desc, // 分享描述
          link: _link, // 分享链接
          imgUrl: _imgUrl, // 分享图标
          type: "link", // 分享类型,music、video或link，不填默认为link
          dataUrl: "", // 如果type是music或video，则要提供数据链接，默认为空
          success: function() {
            Toast("分享成功");
          },
          cancel: function() {
            Toast("取消分享");
          }
        });
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.packageId = this.$route.params.id;
    this.clientWidths = document.body.clientWidth;
    this.getpackage();
    this.giftData();
  }, //如果页面有keep-alive缓存功能，这个函数会触发
}